'use strict';

const Controller = require('egg').Controller;


// const Core = require('@alicloud/pop-core');
// https://help.aliyun.com/document_detail/57342.html
// var client = new Core({
//   accessKeyId: 'LTAIOL60W1RI80wB',
//   accessKeySecret: 'IKH9qiA0Cl9Pg2YpjhMeIEAdWqltHi',
//   endpoint: 'https://iot.cn-shanghai.aliyuncs.com',
//   apiVersion: '2018-01-20'
// });

// var params = {
//   RegionId: "cn-hangzhou",
//   Items: "{Status: 1}",
//   ProductKey: "a1wlEo7ijKg",
//   DeviceName: "IDPkgFqBNLZFSJzYlhco"
// }

// var requestOption = {
//   method: 'POST'
// };

// client.request('SetDeviceProperty', params, requestOption).then((result) => {
//   console.log(JSON.stringify(result));
// }, (ex) => {
//   console.log('ex = ', ex);
// })


class UserController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = {
      user: '朱明',
      id: ctx.query.id,
    };
  }
  async pai() {
    const { ctx } = this;
    let id = ctx.query.id;
    if (id == 1) {
      params.Items = "{Status: 1}";
    } else {
      params.Items = "{Status: 0}";
    }
    let ret = {};
    let ret2 = await client.request('SetDeviceProperty', params, requestOption)
    // client.request('SetDeviceProperty', params, requestOption).then((result) => {
    //   ret = result;
    //   console.log('result = ',result);
    //   if (result.Success) {
    //     ctx.body = {
    //       msg: '成功了!',
    //       // result: result
    //     } 
    //   }
    // }, (ex) => {
    //   console.log('ex = ', ex);
    // })
    if (ret2.Success) {
      ctx.body = {
        msg: '成功了!' + id,
        ret2: ret2
      } 
    }
  }
}

module.exports = UserController;
